<template>
  <div>
    <el-card class="mb">
      <div class="title mb">会议纪要</div>
      <el-form :inline="true" label-width="100px" :model="form">
        <el-form-item label="会议名称：">
          <el-input
            v-model="form.meetingName"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="会议类型：">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in meetingTypeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会议时间：">
          <el-date-picker
            v-model="form.meetingTime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <MeetingTable
        :tableData="tableData"
        :tabtitle="tabtitle"
        :loading="loading"
        height="calc(100vh - 270px)"
        :current="pageNum"
        :pageSize="pageSize"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        sortProp="meetingTime"
      >
        <el-table-column align="center" label="操作" v-if="!onlyLook">
          <template slot-scope="scope">
            <el-button type="text" @click="dlalogOpen(scope.row)"
              >上传</el-button
            >
          </template>
        </el-table-column>
      </MeetingTable>
    </el-card>
    <UploadDialog ref="DialogTable"></UploadDialog>
  </div>
</template>

<script>
import MeetingTable from '@/components/Table.vue'
import UploadDialog from './components/UploadDialog.vue'
import { findMeetingByFile } from '@/apis/meetingMinutes.js'
import { getDetailByAccountOrId, getUserInfo, getSelf } from '@/apis/Common.js'
import { getRealToken } from '@/utils/util'

export default {
  components: {
    MeetingTable,
    UploadDialog
  },
  data() {
    return {
      meetingTypeList: [
        { name: '月度会议', code: '0' },
        { name: '季度会议', code: '1' },
        { name: '年度会议', code: '2' },
        { name: '临时会议', code: '3' }
      ],
      form: {
        meetingName: '',
        type: '',
        meetingTime: ''
      },
      codeList: [],
      tableData: [],
      loading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      onlyLook: false,
      tabtitle: [
        { prop: 'meetingName', label: '会议名称', align: 'center' },
        { prop: 'typeDesc', label: '会议类型', align: 'center' },
        {
          prop: 'masterUserName',
          label: '会议负责人',
          align: 'center'
        },
        {
          prop: 'meetingTime',
          label: '会议时间',
          align: 'center',
          sortable: true
        },
        {
          prop: 'fileState',
          label: '状态',
          render: (h, scope) => {
            return h(
              'div',
              {
                attrs: {
                  class: scope.row.fileState
                    ? scope.row.fileState === '0'
                      ? 'type-normal'
                      : 'type-warning'
                    : ''
                }
              },
              scope.row.fileStateDesc
            )
          }
        }
      ]
    }
  },
  mounted() {
    this.getTableData()
    this.getUserRole()
  },
  methods: {
    async getUserRole() {
      try {
        const accessObj = getRealToken()
        const isCheckOut = accessObj?.isCheckOut
        let res = null
        if (isCheckOut) {
          const resInfo = await getSelf()
          res = await getUserInfo(resInfo.tenantId)
        } else {
          res = await getDetailByAccountOrId({
            account: sessionStorage.getItem('currentUser')
              ? JSON.parse(sessionStorage.getItem('currentUser')).account
              : null
          })
        }
        if (res?.role?.roleCode?.length > 0) {
          const roleCode = res.role.roleCode.split('|')
          const arr = roleCode
            .map((roleCode) =>
              ['hygczmkcy-gcy', 'hygczmkjt-gcy'].indexOf(roleCode)
            )
            .filter((index) => index !== -1)
          if (arr.length > 0) {
            this.onlyLook = true
          } else {
            this.onlyLook = false
          }
        } else {
          this.onlyLook = true
        }
      } catch (error) {
        this.onlyLook = true
      }
    },
    getTableData() {
      this.loading = true
      const { pageSize, pageNum } = this
      findMeetingByFile({ ...this.form, pageSize, pageNum })
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.data.list
            this.total = res.data.total
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    search() {
      this.pageNum = 1
      this.getTableData()
    },

    reset() {
      this.form = this.$options.data().form
      this.search()
    },
    dlalogOpen(row) {
      this.$refs.DialogTable.show(row)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getTableData()
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  font-size: 20px;
  font-weight: bold;
}
.mb {
  margin-bottom: 15px;
}
</style>
